package hero.mps.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import hero.mps.constant.RedisConstant;
import hero.mps.dao.SetMealDao;
import hero.mps.entity.PageResult;
import hero.mps.entity.QueryPageBean;
import hero.mps.pojo.SetMeal;
import hero.mps.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查组服务
 */
@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private JedisPool jedisPool;


    @Override
    public void add(SetMeal setMeal, Integer[] checkGroupIds) {
        //新增检查组
        setMealDao.add(setMeal);
        //设置检查组和检查项之间的关联关系
        Integer setMealId = setMeal.getId();
        setSetMealAndCheckGroup(setMealId, checkGroupIds);
        savePic2Redis(setMeal.getImg());

    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<SetMeal> page = setMealDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }
    @Override
    public SetMeal findById(Integer id) {
        return setMealDao.findById(id);
    }

    @Override
    public void edit(SetMeal setMeal, Integer[] checkGroupIds) {
        //修改检查组基本信息，操作检查组表
        setMealDao.edit(setMeal);
        savePic2Redis(setMeal.getImg());
        //清理当前检查组关联的检查项，操作中间关系表
        setMealDao.deleteAssociation(setMeal.getId());
        //重新建立当前检查组和检查项的关联关系
        Integer setMealId = setMeal.getId();
        setSetMealAndCheckGroup(setMealId, checkGroupIds);
    }

    @Override
    public List<Integer> findCheckGroupIdsBySetMealId(Integer id) {
        return setMealDao.findCheckGroupIdsBySetMealId(id);
    }
    @Override
    public void deleteBySetMealId(Integer id) {
        setMealDao.deleteAssociation(id);
        setMealDao.deleteSetMealById(id);
    }

    //向中间表(t_checkgroup_checkitem)插入数据（建立检查组和检查项关联关系）
    public void setSetMealAndCheckGroup(Integer setMealId,Integer[] checkGroupIds){
        if (checkGroupIds != null && checkGroupIds.length > 0){
            for (Integer checkGroupId : checkGroupIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("setMealId", setMealId);
                map.put("checkGroupId", checkGroupId);
                setMealDao.setSetMealAndCheckGroup(map);
            }
        }
    }
    //将图片名称保存到Redis
    private void savePic2Redis(String pic){
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,pic);
    }

}
